package com.dianxun.exam.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dianxun.exam.MainApplication;
import com.dianxun.exam.R;
import com.dianxun.exam.common.base.BaseActivity;
import com.dianxun.exam.entity.AnswerBean;
import com.dianxun.exam.entity.AnswerBeanDao;
import com.dianxun.exam.entity.DaoSession;
import com.dianxun.exam.entity.ServiceResult;
import com.dianxun.exam.net.Api;
import com.dianxun.exam.net.HttpListener;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

import butterknife.BindView;

/**
 * 上传试卷
 */
public class UploadActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tv_back)
    TextView tv_back;
    @BindView(R.id.list_paper)
    RecyclerView list_paper;
    @BindView(R.id.btn_cancel)
    Button btn_cancel;
    @BindView(R.id.btn_upload)
    Button btn_upload;
    private DaoSession daoSession;
    private AnswerBeanDao answerBeanDao;
    private List<AnswerBean> answerBeanList;
    private PaperAdapter adapter;

    @Override
    protected int getContentView() {
        return R.layout.activity_upload;
    }

    @Override
    protected void initView() {
        daoSession = ((MainApplication) getApplication()).getDaoSession();
        answerBeanDao = daoSession.getAnswerBeanDao();
        answerBeanList = answerBeanDao.queryBuilder().where(AnswerBeanDao.Properties.IsUpload.eq(false))
                .list();

        list_paper.setLayoutManager(new LinearLayoutManager(self));
        adapter = new PaperAdapter();
        list_paper.setAdapter(adapter);
        adapter.setNewData(answerBeanList);
    }

    @Override
    protected void initListeners() {
        tv_back.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.tv_back:
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_upload:
                if (answerBeanList.size() == 0) {
                    toast("没有答案，无法上传");
                    return;
                }
                Gson gson = new GsonBuilder()
                        .setExclusionStrategies(new ExclusionStrategy() {
                            @Override
                            public boolean shouldSkipField(FieldAttributes f) {
                                return (f.getName().equals("id") || f.getName().equals("displayName")
                                        || f.getName().equals("paperNo")|| f.getName().equals("paperName")
                                        || f.getName().equals("isUpload"));
                            }

                            @Override
                            public boolean shouldSkipClass(Class<?> clazz) {
                                return false;
                            }
                        })
                        .create();
                String answerListJson = gson.toJson(answerBeanList);
                Api.save(self, answerListJson, new HttpListener<ServiceResult>() {
                    @Override
                    public void onSucceed(ServiceResult response) {
                        if (response.getCode()!= null && response.getCode().equals("1")) {
                            if (TextUtils.isEmpty(response.getMessage())) {
                                toast("上传成功");
                            }
                            else {
//                                toast(response.getMessage() + "已考过试，其余上传成功");
                                toast("上传成功");
                            }
                            for (int i=0; i<answerBeanList.size(); i++) {
                                answerBeanList.get(i).setIsUpload(true);
                            }
                            answerBeanDao.updateInTx(answerBeanList);
                            finish();
                        } else {
                            toast(response.getMessage());
                        }
                    }

                    @Override
                    public void onFailed() {

                    }
                }, ServiceResult.class);
                break;
        }
    }

    public class PaperAdapter extends BaseQuickAdapter<AnswerBean, BaseViewHolder> {

        public PaperAdapter() {
            super(R.layout.layout_paper_item, null);
        }

        @Override
        protected void convert(BaseViewHolder helper, AnswerBean item) {
            helper.setText(R.id.tv_display_name, item.getDisplayName());
            helper.setText(R.id.tv_endtime, item.getEndTime());
            helper.setText(R.id.tv_paper_no, item.getPaperNo());
            helper.setText(R.id.tv_paper_name, item.getPaperName());
        }
    }
}